package com.my.okhttps.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * xml解析工具类
 */
public class XmlUtils {
    /**
     * xml字符串转对象
     *
     * @param clazz
     * @param xmlStr
     * @return
     */
    public static Object xmlStrToObject(Class clazz, String xmlStr) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // 进行将Xml转成对象的核心接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;
    }

    /**
     * 对象转xml字符串
     *
     * @param obj
     * @param load
     * @return
     * @throws JAXBException
     */
    public static String objectToXmlStr(Object obj, Class<?> load) {
        String result = "";
        try {
            JAXBContext context = JAXBContext.newInstance(load);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);//去掉生成xml的默认报文头
//            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//格式化xml格式
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
            StringWriter writer = new StringWriter();
            marshaller.marshal(obj, writer);//java 对象生成到writer
            result = writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    //转化
    public static void main222(String[] args) {
        //签到
        String xmlStr = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" +
                "<bocb2e version=\"100\" security=\"true\" lang=\"chs\">" +
                "<head>" +
                "<termid>E127000000001</termid>" +
                "<trnid>20060704001</trnid>" +
                "<custid>12345678</custid>" +
                "<cusopr>BOC</cusopr>" +
                "<trncod>b2e0001</trncod>" +
                "</head>" +
                "<trans>" +
                "<trn-b2e0001-rq>" +
                "<b2e0001-rq>" +
                "<custdt>20060704091000</custdt>" +
                "<oprpwd>88888888</oprpwd>" +
                "<ceitinfo>MIIE9AYJKoZIhvcNA……</ceitinfo>" +
                "</b2e0001-rq>" +
                "</trn-b2e0001-rq>" +
                "</trans>" +
                "</bocb2e>";
//        BOCSignRequestXml requestXml = (BOCSignRequestXml) XmlUtils.xmlStrToObject(BOCSignRequestXml.class, xmlStr);
//        System.out.println(requestXml.getTrans().getTrnB2e0001Rq()
//                .getB2e0001Rq().getOprpwd());
//        System.out.println(requestXml.getHead().getTrncod());

//        InputXml inputXml = new InputXml();
//        inputXml.setUserName(name);
//        inputXml.setIdCard(idCard);
//        inputXml.setJybh(GET_CARD_INFO);
//        String xmlStrTo = XmlUtils.objectToXmlStr(inputXml,InputXml.class);

    }
}

